//
//  TradeMsgRequest.swift
//  Exchange
//
//  Created by rui on 2018/6/30.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TradeMsgViewModel: BaseViewModel {
    
    ///是否有未读消息（总的)
    var isNewMessage = false
    ///法币完成的订单是否有未读消息
    var finishTradeOrder = false
    ///法币进行中订单是否有未读消息
    var underwayTradeOrder = false
    ///待下单列表是否有未读消息
    var waitOrder = false
    
    private lazy var tradeMsgApi: TradeMsgRequest = {
        let api = TradeMsgRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    ///加载是否有未读消息
    func loadUnReadMsg() {
        self.tradeMsgApi.loadData()
    }

}


extension TradeMsgViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
       if let _ = request as? TradeMsgRequest {
        if let dt = data?["data"] as? [String:Any] {
            if let isNewMessage = dt["isNewMessage"] as? Bool {
                self.isNewMessage = isNewMessage
            }else {
                self.isNewMessage = false
            }
            if let waitOrder = dt["waitOrder"] as? Bool {
                self.waitOrder = waitOrder
            }else {
                self.waitOrder = false
            }
            if let finishTradeOrder = dt["finishTradeOrder"] as? Bool {
                self.finishTradeOrder = finishTradeOrder
            }else {
                self.finishTradeOrder = false
            }
            if let underwayTradeOrder = dt["underwayTradeOrder"] as? Bool {
                self.underwayTradeOrder = underwayTradeOrder
            }else {
                self.underwayTradeOrder = false
            }
        }
        self.response?(ResponseState.trade_trade_unreadMsg,true, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
      
    }
    
    func requestDidFail(_ request: BaseRequest) {
       if let _ = request as? TradeMsgRequest {
            self.response?(ResponseState.trade_trade_unreadMsg,false, nil)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
       return nil
    }
    
}
